package com.roomio.dao.impl;

import java.util.List;

import org.springframework.stereotype.Repository;

import com.roomio.core.dao.impl.GenericJPADAO;
import com.roomio.core.model.RoomioException;
import com.roomio.dao.GroupInfoDAO;
import com.roomio.domain.Group;

@Repository("groupInfoDAO")
public class GroupInfoDAOImpl extends GenericJPADAO<Group, Long> implements GroupInfoDAO {

	public Group findGroupByNameAndUserName(String name, String userName){
		List<Group> list = (List<Group>)findByNamedQuery("group.by.name.and.username", new String[]{"name","userName"},new Object[]{name,userName});
		if(list.size() > 1){
			throw new RoomioException("For one user group name should be unique");
		}else if(list== null || list.size()==0){
			return null;
		}else {
			return list.get(0);
		}	
	}
	
	public List<Group> findGroupsByOwner( String userName){
		return (List<Group>)findByNamedQuery("group.by.username", new String[]{"userName"},new Object[]{userName});
	}
}